﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary1.Common;

namespace ClassLibrary1.UI
{
    public partial class FilmPictureBox : UserControl
    {
        private Image reducedImage;
        private List<Label> ls = new List<Label>();
        public bool Selected { get; set; }
        public FilmPictureBox()
        {
            InitializeComponent();

            pictureBox1.Paint += PictureBox1_Paint;
            string str = @"C:\ProgramData\Cognex\In-Sight\In-Sight Explorer 5.4.1\RecordPlayback\came1\新建文件夹\C44下法兰校准图.BMP";
            reducedImage = ImageClass.GetReducedImage(str, pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = reducedImage;


            for (int i = 0; i < 10; i++)
            {
                Label label = new Label()
                {
                    AutoSize = false,
                    BackColor = SystemColors.Control,
                };
                ls.Add(label);
            }//生成白孔
            Controls.AddRange(ls.ToArray());
            FilmPictureBox_Resize(null, null);
        }

        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (Selected)
            {
                Pen pen = new Pen(Color.Yellow, pictureBox1.Width / 10);
                e.Graphics.DrawRectangle(pen, pictureBox1.DisplayRectangle);
            }
        }

        private void FilmPictureBox_Resize(object sender, EventArgs e)
        {
            Width = Height;//宽高相等

            pictureBox1.Padding = new Padding(pictureBox1.Width / 15);
            double picHeight = (double)Height / 4 * 3;
            double picWidth = Width;
            double picLocationX = 0;
            double picLocationY = (double)Height / 4 / 2;
            pictureBox1.Location = new Point((int)picLocationX, (int)picLocationY);
            pictureBox1.Height = (int)picHeight;
            pictureBox1.Width = (int)picWidth;//调整pictureBox

            double labelGap = Width / 5;
            double labelHeight = Height / 4 / 2 / 2;
            double labelWidth = labelHeight * 1.5;
            double labelFirstX = 0;
            double labelFirstY = labelHeight / 2;
            for (int i = 0; i < 10; i++)
            {
                ls[i].Height = (int)labelHeight;
                ls[i].Width = (int)labelWidth;
                if (i < 5)
                {
                    ls[i].Location = new Point((int)(labelFirstX + i * labelGap), (int)labelFirstY);
                }
                else
                {
                    int j = i - 5;
                    ls[i].Location = new Point((int)(labelFirstX + j * labelGap), (int)labelFirstY + pictureBox1.Height + pictureBox1.Location.Y);
                }
            }//调整白孔
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Selected = !Selected;
            pictureBox1.Invalidate();
        }
    }
}
